/*
 * @Author: xiaosihan
 * @Date: 2024-11-29 23:38:15
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-12-02 06:48:10
 */

import { GoodsData } from '#/type';
import commonService from '@/api/services/commonService';
import goodsUtils from '@/utils/goodsUtils';
import { configure, observable } from 'mobx';
configure({ enforceActions: 'never' });

const goodsModalStore = observable(
  {
    isInint: true,
    open: false,
    async setOpen(open: boolean) {
      if (open && this.isInint) {
        this.isInint = false;
        await this.getCategory();
        await this.getGoodsGroup();
        this.getGoodsData();
      }
      this.open = open;
    },

    //尺码
    activeSize: '',
    setActiveSize(activeSize: string) {
      this.activeSize = activeSize;
    },
    sizes: [] as Array<string>,
    setSizes(sizes: Array<string>) {
      this.sizes = sizes;
    },
    //颜色
    activeColor: '',
    setActiveColor(activeColor: string) {
      this.activeColor = activeColor;
      this.getGoodsData(1);
    },
    colors: [] as Array<string>,
    setColors(colors: Array<string>) {
      this.colors = colors;
    },
    //材质
    activeMaterial: '',
    setActiveMaterial(activeMaterial: string) {
      this.activeMaterial = activeMaterial;
      this.getGoodsData(1);
    },
    materials: [] as Array<string>,
    setMaterials(materials: Array<string>) {
      this.materials = materials;
    },

    //类别
    activeCategory: undefined as number | undefined,
    setActiveCategory(activeCategory: number) {
      this.activeCategory = this.activeCategory === activeCategory ? undefined : activeCategory;
      this.getGoodsData(1);
    },
    category: [] as Array<{ name: string; value: number }>,
    setCategory(category: Array<{ name: string; value: number }>) {
      this.category = category;
    },
    async getCategory() {
      await commonService.getBaseApi().then((res) => {
        this.setCategory(res.suitFor);
        this.setSizes(res.sizes);
        this.setMaterials(res.material);
        this.setColors(res.color);
      });
    },

    //分组相关的参数
    activeGoodsGroup: undefined as undefined | number,
    setSctiveGoodsGroup(activeGoodsGroup: number) {
      this.activeGoodsGroup =
        this.activeGoodsGroup === activeGoodsGroup ? undefined : activeGoodsGroup;
      this.getGoodsData(1);
    },
    goodsGroup: [] as Array<{ id: number; name: string }>,
    setGoodsGroup(goodsGroup: Array<{ id: number; name: string }>) {
      this.goodsGroup = goodsGroup;
    },
    getGoodsGroup() {
      commonService.getCustomApi('/common/designer/getPreProductGroup', {}).then((res) => {
        this.setGoodsGroup(res);
      });
    },

    pageNo: 1, //当前页面
    pageSize: 12, //分页数
    total: 0, //总数
    goodsDatas: [] as Array<GoodsData>, //数据列表
    //请求数据
    async getGoodsData(pageNo: number = 1) {
      this.pageNo = pageNo;
      const res = await commonService.getCustomApi('/common/designer/getPreProductPage', {
        pageNo: this.pageNo,
        pageSize: this.pageSize,
        suitFor: this.activeCategory,
        catalogueId: this.activeGoodsGroup,
        size: this.activeSize,
        color: this.activeColor,
        material: this.activeMaterial,
      });
      const goods = res.list.map((g: any) => goodsUtils.tojson(g));
      this.total = res.totalCount;
      this.goodsDatas = goods; // 请求数据
      this.activeGoodsId = 0;
    },
    //选中商品
    activeGoodsId: 0,
    setActiveGoodsId(activeGoodsId: number) {
      this.activeGoodsId = activeGoodsId;
    },
  },
  {},
  { deep: false },
);

export default goodsModalStore;
